package cn.lxycx.dataterrace.interceptor.validates;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.lxycx.dataterrace.interceptor.AbsValidateInterceptor;
import cn.lxycx.dataterrace.util.FormVerifys;
import cn.lxycx.dataterrace.util.RegexStr;
import com.alibaba.druid.util.Base64;
import com.jfinal.kit.Base64Kit;
import org.apache.log4j.Logger;
import lxycx.util.validate.Validate.Ret;
import net.sf.json.JSONObject;

public class StatementValidate extends AbsValidateInterceptor {
	
	private static Logger log = Logger.getLogger(StatementValidate.class);

	@Override
	public Ret verify(HttpServletRequest req) {
		List<FormVerifys> fl = new ArrayList<FormVerifys>();
		 Map<String,String> param = new LinkedHashMap<String,String>();
		 JSONObject reqparams = new JSONObject();
		 
		 try {
			String data = req.getParameter("data");
			if(data!=null) {
				data = Base64Kit.decodeToStr(data);//
				//data = new String(data.getBytes("GBK"),"UTF-8");
				data = URLDecoder.decode(data,"UTF-8");
				reqparams = JSONObject.fromObject(data);
			}
		 } catch (Exception e) { log.error(e); }
		 
		 String url = req.getRequestURI();
		 if(url.indexOf("statement/save")>=0){//
			 fl.add(new FormVerifys("id", reqparams.optString("id"), true,-101, "无效的ID", RegexStr.ISNUMBER));//
			 fl.add(new FormVerifys("name", reqparams.optString("name"), false,-101, "名称有误",null));//
			 fl.add(new FormVerifys("classid", reqparams.optString("classid"), false,-101, "分类有误",null));//
		
			 
			 //TABLENAME//REMARK
			 fl.add(new FormVerifys("tablename", reqparams.optString("tablename"), true,-101, "无效的表名",null));//
			 fl.add(new FormVerifys("remark", reqparams.optString("remark"), true,-101, "无效的表名",null));//
			 fl.add(new FormVerifys("aidfield", reqparams.optString("aidfield"), true,-101, "无效的主键名",null));//
			 fl.add(new FormVerifys("seqstr", reqparams.optString("seqstr"), true,-101, "无效的序列名",null));//
			 fl.add(new FormVerifys("sqltext", reqparams.optString("sqltext"), true,-101, "查询语句有误",null));//
			 fl.add(new FormVerifys("wheres", reqparams.optString("wheres"), true,-100, "查询条件有误",null));
			 fl.add(new FormVerifys("constraint_select_conf", reqparams.optString("constraint_select_conf"), true,-100, "查询约束有误",null));

			 
			 
			 fl.add(new FormVerifys("inserts", reqparams.optString("inserts"), true,-101, "插入条件有误",null));//
			 fl.add(new FormVerifys("updates", reqparams.optString("updates"), true,-101, "更新条件有误",null));//
			 fl.add(new FormVerifys("buttonconf", reqparams.optString("buttonconf"), true,-101, "按钮名称有误",null));//
		 }
		
		 Ret ret = FormVerifys.Valuedate(fl, param);
		 req.setAttribute("param",param);
		 return ret;
		
	}
	
}
